// pages/user-my-kf-chat/user-my-kf-chat.js
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    userId: "", //用户id
    pageNum: 1,
    pageSize: 10,
    loadLoading: false,
    loadComplete: false,
    talkDetail: [],
    content: "", //提问的内容
    postLoading: false,
    imgUrl: "https://tyc-bj.cn/yc/api/attach/"
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    this.setData({
      userId: options.userId || ''
    });
    this.getTalkDetail();
  },

  getTalkDetail: function() {
    const t = this;
    let { loadLoading, talkDetail } = t.data;
    app.request({
      url: 'consult/talkapi/getTalkListByUserAndDoctor',
      data: {
        pageSize: t.data.pageSize,
        pageNum: t.data.pageNum,
        userId: t.data.userId, //用户id
      },
      success: function(res) {
        const { data, page } = res;
        if (page.current_page >= page.total_page) {
          //没有更多了
          t.setData({
            loadComplete: true
          });
        }
        if (loadLoading) {
          //上拉加载
          t.setData({
            talkDetail: talkDetail.concat(data)
          });
        }
        else{
          t.setData({
            talkDetail: data,
          });
        }
        t.setData({
          loadLoading: false
        });
      },
      fail: function(res) {}
    })
  },

  changeContent: function(e) {
    this.setData({
      content: e.detail.value
    })
  },

  // 回复按钮
  response: function() {
    const t = this;
    let { postLoading, content, doctorId } = t.data;
    if (!postLoading) {
      if (content.length > 0) {
        t.setData({
          postLoading: true
        });
        app.request({
          url: 'consult/talkapi/addtalk',
          data: {
            content: t.data.content, //内容
            userId: t.data.userId, //用户id
            pId: t.data.talkDetail[t.data.talkDetail.length - 1]["id"] || '', //回复id
          },
          success: function (res) {
            t.getTalkDetail();
            t.setData({
              content: ''
            })
          },
          fail: function (res) { }
        });
      }
      else {
        wx.showToast({
          title: '请输入回复内容',
          icon: "none",
          duration: 2000
        });
      }
    }
    else {
      wx.showToast({
        title: '操作繁忙请稍后再试',
        icon: "none",
        duration: 2000
      });
    }
  },

  //上拉加载
  loadMoreData: function () {
    const t = this;
    let { pageNum, loadLoading, loadComplete } = t.data;
    if (!loadLoading && !loadComplete) {
      t.setData({
        pageNum: pageNum + 1,
        loadLoading: true
      });
      t.getTalkDetail();
    }
  }
})